﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace kpi.MasterManager
{
    public partial class UserManagersMgr : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList_ShowNames();
        }

        protected void DropDownList_ShowNames()
        {
            SqlConnection SqlConnection = new SqlConnection("Data Source=192.168.0.241;Initial Catalog=kpi_aspnet_db;Persist Security Info=True;User ID=sa;Password=My.SA");
            SqlConnection.Open();

            String selectCommand = "SELECT [UserId], [UserName] FROM [dbo].[aspnet_Users]";
            SqlCommand SelectCommand = new SqlCommand(selectCommand, SqlConnection);
            SqlDataReader IncUserNameReader = SelectCommand.ExecuteReader();

            while (IncUserNameReader.Read())
            {
                DropDownList_ManagerName.Items.Add(new ListItem(IncUserNameReader["UserName"].ToString(), IncUserNameReader["UserId"].ToString()));
            }

            SqlConnection.Close();
            
            
            //DropDownList_ManagerName.DataSource = Membership.GetAllUsers();           
            //DropDownList_ManagerName.DataBind();

        }

        protected void Button_JoinUserToManager_Click(object sender, EventArgs e)
        {
            //Label_DebugMsg.Text = "INSERT INTO [dbo].[u_kpi_userInManager] (UserId, ManagerId) VALUES ('" + GetUserId("徐津煜") + "', " + "'" + GetUserId("徐津煜") + "')";
            //Label_DebugMsg.Text = GetUserId("徐津煜");

            //JoinUserToManagerInsertQuery(GetUserId("徐津煜"), GetUserId("徐津煜"));
            JoinUserToManager();
        }

        protected void JoinUserToManager()
        {
            String uManagerId = DropDownList_ManagerName.SelectedValue;
            //Label_DebugMsg.Text = uManagerName;

            String[] UserNamesArray = TextBox_UserNames.Text.Split('\n', '\r');
            for (int i = 0; i < UserNamesArray.Length; i++)
            {
                Label_DebugMsg.Text += UserNamesArray[i].ToString();
                if (UserNamesArray[i].ToString() != "")
                {
                    //asp_net_membership_DeleteUser(NamesArray[i].ToString());
                    JoinUserToManagerInsertQuery(GetUserIdByUserNsme(UserNamesArray[i].ToString()), uManagerId);
                }
            }
            TextBox_UserNames.Text = "";
        }

        protected String GetUserIdByUserNsme(String UserName)
        {
            String UserId = "";

            SqlConnection SqlConnection = new SqlConnection("Data Source=192.168.0.241;Initial Catalog=kpi_aspnet_db;Persist Security Info=True;User ID=sa;Password=My.SA");
            SqlConnection.Open();

            String selectCommand = "SELECT [UserId] FROM [dbo].[aspnet_Users] WHERE [UserName]='" + UserName + "'";
            SqlCommand SelectCommand = new SqlCommand(selectCommand, SqlConnection);
            SqlDataReader UserIdReader = SelectCommand.ExecuteReader();

            while (UserIdReader.Read())
            {
                UserId = UserIdReader["UserId"].ToString();
            }            

            SqlConnection.Close();
            
            return UserId;
        }

         protected void JoinUserToManagerInsertQuery(String UserId, String uManagerId)
        {
            SqlConnection SqlConnection = new SqlConnection("Data Source=192.168.0.241;Initial Catalog=kpi_forms_db;Persist Security Info=True;User ID=sa;Password=My.SA"); ;
            SqlConnection.Open();
            SqlCommand insertCmd = null;

            if (UserId != null || uManagerId != null)
            {
                insertCmd = new SqlCommand("INSERT INTO [dbo].[u_kpi_userInManager] (UserId, uManagerId) VALUES ('" + UserId + "', " + "'" + uManagerId + "')", SqlConnection);
                SqlParameter[] SqlParameter = new SqlParameter[2];
                SqlParameter[0] = new SqlParameter("@UserId", SqlDbType.UniqueIdentifier);
                SqlParameter[1] = new SqlParameter("@uManagerId", SqlDbType.UniqueIdentifier);
                SqlParameter[0].Value = new System.Guid(UserId);
                SqlParameter[1].Value = new System.Guid(uManagerId);
                for (int i = 0; i < SqlParameter.Length; i++)
                {
                    insertCmd.Parameters.Add(SqlParameter[i]);
                }
            }
            //else if (DoUserMgrType.Equals("rem"))
            //{
            //    cmd = new SqlCommand("UPDATE [dbo].[u_kpi_table] SET u_kpi_m = NULL WHERE [UserName] = @UserName AND u_kpi_m = @u_kpi_m", SqlConnection);
            //    SqlParameter[] SqlParameter = new SqlParameter[2];
            //    SqlParameter[0] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
            //    SqlParameter[1] = new SqlParameter("@u_kpi_m", SqlDbType.VarChar, 50);
            //    SqlParameter[0].Value = UserName;
            //    SqlParameter[1].Value = User.Identity.Name;
            //    for (int i = 0; i < SqlParameter.Length; i++)
            //    {
            //        cmd.Parameters.Add(SqlParameter[i]);
            //    }
            //}

            insertCmd.CommandType = CommandType.Text;
            insertCmd.ExecuteNonQuery();
            SqlConnection.Close();
        }
    }
};